package com.yuncommunity.runcircle.fragment;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.oldfeel.utils.NetUtil;
import com.yuncommunity.runcircle.R;
import com.yuncommunity.runcircle.base.MyFragment;
import com.yuncommunity.runcircle.conf.JsonApi;
import com.yuncommunity.runcircle.conf.Net;
import com.yuncommunity.runcircle.conf.UserInfo;
import com.yuncommunity.runcircle.db.DBHelper;
import com.yuncommunity.runcircle.item.LocationItem;
import com.yuncommunity.runcircle.view.RunDataView;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import butterknife.Bind;
import butterknife.ButterKnife;

/**
 * Created by oldfeel on 11/22/15.
 */
public class AnalysisFragment extends MyFragment {
    @Bind(R.id.today_distance)
    TextView todayDistance;
    @Bind(R.id.time)
    TextView time;
    @Bind(R.id.calories)
    TextView calories;
    @Bind(R.id.ranking)
    TextView ranking;
    @Bind(R.id.steps)
    TextView steps;
    @Bind(R.id.max_distance)
    TextView maxDistance;
    @Bind(R.id.max_speed)
    TextView maxSpeed;
    @Bind(R.id.max_time)
    TextView maxTime;
    @Bind(R.id.average)
    TextView average;
    @Bind(R.id.max)
    TextView max;
    @Bind(R.id.run_data)
    RunDataView runData;
    @Bind(R.id.day_1)
    TextView day1;
    @Bind(R.id.day_2)
    TextView day2;
    @Bind(R.id.day_3)
    TextView day3;
    @Bind(R.id.day_4)
    TextView day4;
    @Bind(R.id.day_5)
    TextView day5;
    @Bind(R.id.day_6)
    TextView day6;
    @Bind(R.id.day_7)
    TextView day7;
    @Bind(R.id.max_distance_date)
    TextView maxDistanceDate;
    @Bind(R.id.max_time_date)
    TextView maxTimeDate;
    @Bind(R.id.max_speed_date)
    TextView maxSpeedDate;
    private DBHelper dbHelper;
    private UserInfo userInfo;

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.analysis, container, false);
        ButterKnife.bind(this, view);
        return view;
    }

    @Override
    public void onActivityCreated(Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        dbHelper = DBHelper.getInstance(getActivity());
        userInfo = UserInfo.getInstance();

//        init7DayData();
//        init30DayData();
//        initTodayData();

        getSportReport();
    }

    private void getSportReport() {
        Net net = new Net(getActivity(), JsonApi.getSportReport);
        net.sendPost(new NetUtil.OnJsonResultListener() {
            @Override
            public void onSuccess(String data) {
                parseData(data);
            }

            @Override
            public void onFail(int code, String data) {
                showToast("getSportReport 失败");
            }
        });
    }

    private void parseData(String data) {
        try {
            JSONObject json = new JSONObject(data);
            parseMax(json.getJSONObject("max"));
            parseToday(json.getJSONObject("today"));
            parseWeek(json.getJSONObject("week"));
        } catch (JSONException e) {
            e.printStackTrace();
        }
    }

    private void parseMax(JSONObject max) throws JSONException {
        maxDistanceDate.setText(max.getString("distance_date"));
        maxDistance.setText(max.getString("distance"));
        maxTimeDate.setText(max.getString("duration_date"));
        maxTime.setText(max.getString("duration"));
        maxSpeedDate.setText(max.getString("speed_date"));
        maxSpeed.setText(max.getString("speed"));
    }

    private void parseToday(JSONObject today) throws JSONException {
        todayDistance.setText(today.getString("distance"));
        time.setText(today.getString("duration"));
        calories.setText(today.getString("calorie"));
        steps.setText(today.getString("step"));
        ranking.setText(today.getString("rank"));
    }

    private void parseWeek(JSONObject week) throws JSONException {
        LocationItem[] datas = new LocationItem[7];
        JSONArray array = week.getJSONArray("list");
        if (array.length() != 7) {
            showToast("七日跑步数据不是 7 个");
            return;
        }

        double minValue = 9999;
        for (int i = 0; i < datas.length; i++) {
            LocationItem item = new LocationItem();
            double temp = array.getJSONObject(i).getDouble("distance");
            if (minValue > temp) {
                minValue = temp;
            }
            String date = array.getJSONObject(i).getString("date");
            item.date = date;
//            if (i == 0) {
//                item.date = date.replace("-", "月") + "日";
//            } else {
//                item.date = date.substring(date.lastIndexOf("-") + 1);
//            }
            item.distance = temp;
            datas[i] = item;
        }

        max.setText(week.getString("max"));
        average.setText(week.getString("average"));
        runData.setData(datas, week.getDouble("average"), minValue, week.getDouble("max"));
        day1.setText(datas[0].date);
        day2.setText(datas[1].date + "");
        day3.setText(datas[2].date + "");
        day4.setText(datas[3].date + "");
        day5.setText(datas[4].date + "");
        day6.setText(datas[5].date + "");
        day7.setText(datas[6].date + "");
    }

//    private void initTodayData() {
//        String distance = dbHelper.getTodayDistance();
//        todayDistance.setText(distance);
//        time.setText(dbHelper.getTodayRunTime());
//        calories.setText(MyUtil.getCalories(userInfo.getWeight(), Double.valueOf(distance)));
//    }
//
//    private void init30DayData() {
//        LocationItem maxDistanceItem = dbHelper.get30DayMaxDistance();
//        maxDistanceDate.setText(maxDistanceItem.date);
//        maxDistance.setText(new DecimalFormat("0.00").format(maxDistanceItem.distance) + "km");
//
//        LocationItem maxTimeItem = dbHelper.get30DayMaxTime();
//        maxTimeDate.setText(maxTimeItem.date);
//        maxTime.setText(StringUtil.formatTime(maxTimeItem.duration));
//
//
//        LocationItem maxSpeedItem = dbHelper.get30DayMaxSpeed();
//        maxSpeedDate.setText(maxSpeedItem.date);
//        maxSpeed.setText(new DecimalFormat("0.00").format(maxSpeedItem.speed) + "min/km");
//    }
//
//    private void init7DayData() {
//        LocationItem[] datas = dbHelper.getLast7Day();
//        double distanceValue = 0;
//        double minValue = 9999;
//        double maxValue = 0;
//        for (int i = 0; i < datas.length; i++) {
//            double temp = datas[i].distance;
//            if (minValue > temp) {
//                minValue = temp;
//            }
//            if (maxValue < temp) {
//                maxValue = temp;
//            }
//            distanceValue += temp;
//        }
//        double averageValue = distanceValue / 7;
//        runData.setData(datas, averageValue, minValue, maxValue);
//
//        average.setText(new DecimalFormat("0.00").format(averageValue));
//        max.setText(new DecimalFormat("0.00").format(maxValue));
//        day1.setText(datas[0].date);
//        day2.setText(datas[1].day + "");
//        day3.setText(datas[2].day + "");
//        day4.setText(datas[3].day + "");
//        day5.setText(datas[4].day + "");
//        day6.setText(datas[5].day + "");
//        day7.setText(datas[6].day + "");
//    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        ButterKnife.unbind(this);
    }
}
